package com.jokerpan.project.controller;

import com.jokerpan.project.annotation.AccessLimit;
import com.jokerpan.project.annotation.LogAction;
import com.jokerpan.project.commons.ActionResult;
import com.jokerpan.project.commons.ListResult;
import com.jokerpan.project.commons.ResultBuilder;
import com.jokerpan.project.entity.Users;
import com.jokerpan.project.entity.Visitors;
import com.jokerpan.project.service.UsersService;
import com.jokerpan.project.service.VisitorsService;
import com.jokerpan.project.util.IpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author jokerpan
 */
@RestController
@RequestMapping("visitors")
@Api("访问者IP获取Api")
public class VisitorsController {
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @Resource
    VisitorsService visitorsService;
    @Resource
    public UsersService usersService;

    /**
     * 查询所有数据
     * @return 对象列表
     */
    @GetMapping("/queryList")
    @PreAuthorize("hasPermission('','admin:list')")
    @AccessLimit(seconds = 6, maxCount = 5)
    @ApiOperation("查询所有访客记录")
    public ListResult<Object> queryList(Visitors visitors){
        return ResultBuilder.buildListSuccess(visitorsService.queryList(visitors));
    }

    @RequestMapping("/getIpAddress")
    @ApiOperation("获取当前访问者IP地址并增加访客记录")
    @AccessLimit(seconds = 6, maxCount = 5)
    public ListResult<Object> getIpAddress(HttpServletRequest request, Principal principal){
        String ip = IpUtil.getRealIP(request);
        String address = IpUtil.GetAddressByIp(ip);
        Users users = null;
        if (!address.equals("error")){
            if (principal != null){
                String username = principal.getName();
                if (username != null){
                    users = usersService.checkLoginName(username);
                }
            }
            Visitors visitors = new Visitors();
            visitors.setUserId(users != null ? users.getId() : 0);
            visitors.setIp(ip);
            visitors.setAddress(address);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            visitors.setTime(format.format(new Date()));
            if (visitorsService.add(visitors)){
                log.info("访客记录增加成功！");
            }else {
                log.info("访客记录增加失败！");
            }
        }else {
            log.info("访客记录增加失败！");
        }
        return ResultBuilder.buildListSuccess(ip);
    }

    @GetMapping("/deleteById")
    @PreAuthorize("hasPermission('','admin:delete')")
    @LogAction("访客记录,删除访客记录,删除记录,删除")
    @ApiOperation("根据id删除记录")
    @AccessLimit(seconds = 6, maxCount = 5)
    public ActionResult deleteById(@RequestParam("id") Integer id){
        if (visitorsService.deleteById(id)){
            return ResultBuilder.buildActionSuccess();
        }else {
            return ResultBuilder.buildActionFailed();
        }
    }

}
